(function ($) {
    $.extend({
        ajax_handler: function (options) {
            this.defaults = {
				m: 'index',
				a: 'index',
				plus: {},
				callback: function(json){
					alert(json.error);
				}
			};
			var opts = $.extend({}, this.defaults, options);
			
			var url = '?h=1&m='+opts.m+'&a='+opts.a;
			$.each(opts.plus,function(index,val){url = url +'&'+index+'='+val});
			$.getJSON(url,{},function(json){
				opts.callback.apply(this,arguments);
			});
        }
    })
})(af);


/*=======================*/
var my_uuid;

function get_my_uuid() {
    var storage = window.localStorage;
    my_uuid = storage.getItem("my_uuid");
    if (my_uuid==undefined) {
        my_uuid=$.uuid();
        storage.setItem("my_uuid", my_uuid);
    }
    console.log(my_uuid);
}

function check_uuid() {
    if (my_uuid==undefined) {
        $("#my_uuid").html('checking for uuid...');
        window.setTimeout(check_uuid,100);
    } else {
        $("#my_uuid").html('uuid:'+my_uuid);
    }
}

function send_login() {
    $.ajax_handler({m:'account',a:'test_login',plus:{uuid:my_uuid},callback:function(json){
		window.location.href=json.url;	
	}});
}

function send_demo(options) {
	this.defaults = {
		m: 'index',
		a: 'index',
		plus: {},
		callback: function(json){
			alert(json.error);
		}
	};
	var opts = $.extend({}, this.defaults, options);
	
	var url = 'test.php?m='+opts.m+'&a='+opts.a;
	$.each(opts.plus,function(index,val){url = url +'&'+index+'='+val});
	$.get(url,function(data){
		$("#demo_target").html(data);
	});
}

var socket;

function s_init(){
  var host = "ws://localhost:12345";
  try{
    socket = new WebSocket(host);
    log('WebSocket - status '+socket.readyState);
    socket.onopen    = function(msg){ log("Welcome - status "+this.readyState); };
    socket.onmessage = function(msg){ log("Received: "+msg.data); };
    socket.onclose   = function(msg){ log("Disconnected - status "+this.readyState); };
  }
  catch(ex){ log(ex); }
  $("msg").focus();
}

function send(){
  var txt,msg;
  txt = $("msg");
  msg = txt.value;
  if(!msg){ alert("Message can not be empty"); return; }
  txt.value="";
  txt.focus();
  try{ socket.send(msg); log('Sent: '+msg); } catch(ex){ log(ex); }
}
function quit(){
  log("Goodbye!");
  socket.close();
  socket=null;
}

// Utilities
function log(msg){ $("log").innerHTML+="<br>"+msg; }
function onkey(event){ if(event.keyCode==13){ send(); } }